import {getCategoryAPI} from '@/apis/category'
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { onBeforeRouteUpdate } from 'vue-router';

export function useCategory(){
  //分类页数据
  const categoryData = ref({})
  const route = useRoute()
  const getCategoryData = async(id = route.params.id)=>{
    const {data:res} = await getCategoryAPI( id )
    categoryData.value = res.result
  }
  onMounted( () => getCategoryData() )

  //路由参数变化时 可以把分类数据接口重新发送
  onBeforeRouteUpdate((to)=>{
    getCategoryData(to.params.id)
  })

  return {
    categoryData
  }
}
